﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace XNAGame_Lab1
{
    public class MyXNAButton : VisibleGame
    {
        public event EventHandler Clicked;
        // Gets fired when the button is held down.
        public event EventHandler OnPress;
        int _Cost;

        public int Cost
        {
            get { return _Cost; }
            set { _Cost = value; }
        }

        public MyXNAButton(ContentManager content, List<string> strTexture, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);


        }
        public MyXNAButton() { }
        //  /abc/dass/hinh1
        private void InitMyXNAButton(ContentManager content, List<string> strTexture, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            TopLeft = topleft;
            Size = size;
            nSprites = strTexture.Count;
            Sprites = new List<MySprites2D>();
            Texture2D[] texture = new Texture2D[nSprites];
            for (int i = 0; i < nSprites; i++)
                texture[i] = content.Load<Texture2D>(strTexture[i]);

            MySprites2D mySprite = new MySprites2D(texture, topleft);
            Sprites.Add(mySprite);
            this.nSprites = Sprites.Count;

        }
        public MyXNAButton(ContentManager content, string refix, int nSprites, Vector2 topleft, Vector2 size)
        {
            InitWithRefix(content, refix, nSprites, ref topleft, ref size);

        }

        private void InitWithRefix(ContentManager content, string refix, int nSprites, ref Vector2 topleft, ref Vector2 size)
        {
            List<string> strTexture = new List<string>(nSprites);
            for (int i = 0; i < nSprites; i++)
                strTexture.Add(refix + (i).ToString("00"));
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);

        }
        public MyXNAButton(ContentManager content, string strXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(strXML);
            XmlNodeList root = doc.GetElementsByTagName("buttons");
            XmlNode parent = root.Item(0);
            XmlNode button = parent.ChildNodes.Item(0);
            XmlNode texture = button.ChildNodes.Item(4);
            List<string> strTexture = new List<string>(15);
            //            int nSprites = 1;
            Vector2 topleft = new Vector2();
            Vector2 size = new Vector2();
            XmlNode X = button.ChildNodes.Item(0);
            XmlNode Y = button.ChildNodes.Item(1);
            XmlNode W = button.ChildNodes.Item(2);
            XmlNode H = button.ChildNodes.Item(3);

            topleft.X = int.Parse(X.InnerText);
            topleft.Y = int.Parse(Y.InnerText);
            size.X = int.Parse(W.InnerText);
            size.Y = int.Parse(H.InnerText);

            for (int i = 0; i < 15; i++)
            {
                XmlNode texture1 = texture.ChildNodes.Item(i);
                strTexture[i] = texture1.InnerText;

            }
            InitMyXNAButton(content, strTexture, nSprites, ref topleft, ref size);
            //            XmlNodeList button = buttons.("button");


        }

        public enum ButtonStatus
        {
            not,
            Normal,
            MouseOver,
            Pressed,
        }
        private MouseState previousState;

        public MouseState PreviousState
        {
            get { return previousState; }
            set { previousState = value; }
        }
        private ButtonStatus state = ButtonStatus.Normal;

        public ButtonStatus State
        {
            get { return state; }
            set { state = value; }
        }
        
        // Gets fired when the button is pressed.
        bool isMouseOver;

        public override void Update(GameTime gameTime)
        {

            MouseState mouseState = Mouse.GetState();
            if (mouseState.LeftButton==ButtonState.Pressed&& this.isClicked(new Vector2(mouseState.X, mouseState.Y)))
                isMouseOver = true;

            if (mouseState.LeftButton == ButtonState.Pressed &&
previousState.LeftButton == ButtonState.Released)
            {
                if (isMouseOver == true)
                {
                    // Update the button state.
                    state = ButtonStatus.Pressed;
                }
                if (OnPress != null)
                {
                    // Fire the OnPress event.
                    OnPress(this, EventArgs.Empty);
                }
            }
            if (mouseState.LeftButton == ButtonState.Released &&
                previousState.LeftButton == ButtonState.Pressed)
            {
                if (isMouseOver == true)
                {
                    // update the button state.
                    state = ButtonStatus.MouseOver;

                    if (Clicked != null)
                    {
                        // Fire the clicked event.
                        Clicked(this, EventArgs.Empty);
                    }
                }

                else if (state == ButtonStatus.Pressed)
                {
                    state = ButtonStatus.Normal;
                }
                isMouseOver = false;
            }
            previousState = mouseState;
            
        
            base.Update(gameTime);
        }
      
    }
}
